
import pytest
from api.ihrm_emp_curd import IhrmEmpCURD
from common.get_headers import get_headers
from common.read_json_util import read_json_data
from common.text import generate_unique_mobile



class TestEmp:

    def setup_method(self):
        self.authorization = get_headers().get("Authorization")


    # 使用pytest的参数化方式
    @pytest.mark.parametrize("desc,json_data,status_code,success,code,message",
                             read_json_data("data/text_emp.json"))
    def test_add_emp(self, desc, json_data, status_code, success, code, message):

        # 准备数据
        header = {"Content-Type": "application/json", "Authorization": f"Bearer {self.authorization}"}

        # 如果JSON数据中没有提供手机号，则自动生成一个
        if not json_data.get("mobile"):
            mobile = generate_unique_mobile()
            json_data["mobile"] = mobile
            print(f"自动生成手机号: {json_data['mobile']}")

        # 调用自己的封装
        resp = IhrmEmpCURD.add_emp(header, json_data)
        print(desc, " :", resp.json())

        # 断言
        assert resp.status_code == status_code
        assert resp.json().get("success") == success
        assert resp.json().get("code") == code
        assert resp.json().get("message").count(message) > 0

    def test_002(self):
        print(222)

    def test_003(self):
        print(333)